/* --------------------------------------------------------
 * Copyright (c) aeky , Inc.  All rights reserved.
 * --------------------------------------------------------
 */
package com.aeky.client.view.welcome;

import com.aeky.client.common.DemoConstants;
import com.aeky.client.common.DemoEvents;
import com.aeky.client.common.Registry;
import com.aeky.client.mvc.AppEvent;
import com.aeky.client.mvc.Controller;
import com.aeky.client.mvc.View;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.Side;
import com.smartgwt.client.types.TreeModelType;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.ImgButton;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.Layout;
import com.smartgwt.client.widgets.tab.Tab;
import com.smartgwt.client.widgets.tab.TabSet;
import com.smartgwt.client.widgets.tree.Tree;
import com.smartgwt.client.widgets.tree.TreeGrid;
import com.smartgwt.client.widgets.tree.TreeGridField;
import com.smartgwt.client.widgets.tree.TreeNode;

/**
 * 
 * @author aeky
 */
public class WestPanelView extends View {

    private HLayout rollOverCanvas;

    private ListGridRecord rollOverRecord;

    private Layout westPanel;

    public WestPanelView(Controller controller) {
        super(controller);
        westPanel = Registry.<Layout> get(DemoConstants.WEST_PANEL);
        this.instanceWidgets();
    }

    private void instanceWidgets() {
        TabSet tab = new TabSet();
        tab.setTabBarPosition(Side.TOP);
        Tab tTab1 = new Tab("主菜单");
        Tab tTab2 = new Tab("在线人员");
        Tab tTab3 = new Tab("全部人员");
        tab.addTab(tTab1);
        tab.addTab(tTab2);
        tab.addTab(tTab3);

        Tree employeeTree = new Tree();
        employeeTree.setModelType(TreeModelType.CHILDREN);
        employeeTree.setNameProperty("Name");
        employeeTree.setChildrenProperty("directReports");
        employeeTree.setRoot(employeeRoot);

        TreeGrid employeeTreeGrid = new TreeGrid() {
            protected Canvas getRollOverCanvas(Integer rowNum, Integer colNum) {
                rollOverRecord = this.getRecord(rowNum);
                if (rollOverCanvas == null) {
                    rollOverCanvas = new HLayout(3);
                    rollOverCanvas.setSnapTo("TR");
                    rollOverCanvas.setWidth(50);
                    rollOverCanvas.setHeight(22);
                    ImgButton editImg = new ImgButton();
                    editImg.setShowDown(false);
                    editImg.setShowRollOver(false);
                    editImg.setLayoutAlign(Alignment.CENTER);
                    editImg.setSrc("msg.png");
                    editImg.setPrompt("发送短消息");
                    editImg.setHeight(16);
                    editImg.setWidth(16);
                    editImg.addClickHandler(new ClickHandler() {
                        public void onClick(ClickEvent event) {
                            SC.say("Edit Comment Icon Clicked for country : "
                                    + rollOverRecord.getAttribute("countryName"));
                        }
                    });

                    ImgButton chartImg = new ImgButton();
                    chartImg.setShowDown(false);
                    chartImg.setShowRollOver(false);
                    chartImg.setLayoutAlign(Alignment.CENTER);
                    chartImg.setSrc("email.png");
                    chartImg.setPrompt("发送电子邮件");
                    chartImg.setHeight(16);
                    chartImg.setWidth(16);
                    chartImg.addClickHandler(new ClickHandler() {
                        public void onClick(ClickEvent event) {
                            SC.say("Chart Icon Clicked for country : " + rollOverRecord.getAttribute("countryName"));
                        }
                    });

                    rollOverCanvas.addMember(editImg);
                    rollOverCanvas.addMember(chartImg);
                }
                return rollOverCanvas;
            }
        };
        employeeTreeGrid.setWidth100();
        employeeTreeGrid.setShowRollOverCanvas(true);
        employeeTreeGrid.setHeight100();
        employeeTreeGrid.setNodeIcon("preference.gif");
        employeeTreeGrid.setFolderIcon("help.gif");
        employeeTreeGrid.setShowHeader(false);
        employeeTreeGrid.setShowOpenIcons(false);
        employeeTreeGrid.setShowDropIcons(false);
        employeeTreeGrid.setAnimateFolders(false);
        employeeTreeGrid.setClosedIconSuffix("");
        employeeTreeGrid.setFields(new TreeGridField("Name"));
        employeeTreeGrid.setData(employeeTree);
        tTab3.setPane(employeeTreeGrid);
        tTab1.setPane(new MainMenuTabContent().getMainMenuTreeGrid());
        westPanel.addMember(tab);
    }

    @Override
    protected void handleEvent(AppEvent event) {
        if (event.getType() == DemoEvents.INIT) {
            System.out.println("init event do");
        }
    }

    public static final TreeNode employeeRoot = new EmployeeTreeNode("1", "Root", new EmployeeTreeNode("4",
            "Charles Madigen", new EmployeeTreeNode("188", "Rogine Leger"), new EmployeeTreeNode("189", "Gene Porter",
                    new EmployeeTreeNode("265", "Olivier Doucet"), new EmployeeTreeNode("264", "Cheryl Pearson"))));

    public static class EmployeeTreeNode extends TreeNode {
        public EmployeeTreeNode(String employeeId, String name) {
            this(employeeId, name, new EmployeeTreeNode[] {});
        }

        public EmployeeTreeNode(String employeeId, String name, EmployeeTreeNode... children) {
            setAttribute("EmployeeId", employeeId);
            setAttribute("Name", name);
            setAttribute("directReports", children);
        }
    }
}
